package com.weir.workdemo.work01.limit;

import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author weir
 */
@SuppressWarnings("UnstableApiUsage")
@RestController
@Slf4j(topic = "limiter")
public class LimiterController {

    private final RateLimiter limiter = RateLimiter.create(2.0);

    @GetMapping("/limit")
    public String limit() {
        if (!limiter.tryAcquire()) {
            return "系统繁忙，请稍后再试！";
        }
        return "业务处理成功！";
    }

    @GetMapping("/limit2")
    @Limit(key = "limit2", description = "limit2接口限流", period = 0, count = 0)
    public String limit2() {
        return "业务处理成功！";
    }
}
